<?php
global $_W,$_GPC;
load()->func('tpl');
	$rid=$_GPC["rid"];
	$acid=$_W['uniacid'];
	
	$openid = $_W["openid"];
	$nick=$_W['fans']['nickname'];
	$headimg=$_W['fans']['tag']['avatar'];
	$reply=pdo_fetch("SELECT * FROM " . tablename('dt_question_reply') . " WHERE rid = :rid ", array(':rid' => $rid));
	
	$userinfo = pdo_fetch("SELECT * FROM ". tablename("dt_userpoints")." WHERE openid=:openid and rid=:rid ORDER BY ID DESC LIMIT 1" , array(":openid"=>$openid,":rid"=>$rid));
	if(!empty($userinfo)){
		$id=intval($userinfo["id"]);
	}
	//排行榜显示数
	$phbnum=(!empty($reply["phbnum"]) && $reply["phbnum"]!=0)?$reply["phbnum"]:100;
	$phbimg=!empty($reply["phbimg"])?$_W['attachurl'].$reply["phbimg"]:MODULE_URL."resource/images/pic/ranking.png";
	$phbnum=30;
	//排行榜
	//$userlist=pdo_fetchall("select * from ". tablename('dt_userinfo')." where rid=:rid and sytime>0 order by points desc,sytime asc limit ".$phbnum, array(':rid' => $rid));
	$usersum=count($userlist);
	$userlist=pdo_fetchall("SELECT sum(points) sum_points, count(*) count_poins,openid,nick,headimg FROM ". tablename('dt_userpoints')." WHERE  rid=:rid
			and status=:status and points>0 group by openid order by sum_points desc " , array(":rid"=>$rid,":status"=>1));
//	print_r($userlist); die();
	
	
	//好友列表
	$helplist=pdo_fetchall("select * from ". tablename('dt_userinfo')." where openid in (select openid from  ". tablename('dt_usershare')." where fromuserid=:from and rid=:rid) and rid=:rid GROUP BY openid ORDER BY points DESC,sytime",array(":rid"=>$rid,":from"=>$openid));
	$helpcount=count($helplist);

	//倒计时
	$endsecond=$reply["end_time"];
	$now = time();
	$leftsecond=$endsecond-$now;
	//奖励设置
	$isjl=$reply["isjl"];
	$jltype=$reply["jltype"];
	$jlnum=$reply["jlnum"];
	$jlcontent=$reply["jlcontent"];
	//分享
	$sharetitle=$reply["sharetitle"];//分享标题
	$sharecontent=$reply["sharecontent"];//分享内容
	$shareimg=$_W["attachurl"].$reply["shareimg"];//分享图片
	$isshareadd=$reply["isshareadd"];//是否 分享增加一次
	$sharelink="";
	$color=$reply["color"];
	if($isshareadd==0){
		$sharelink = $_W ['siteroot'] . 'app/' . $this->createMobileUrl ( 'index', array ('rid' => $rid));
	}else{
		$sharelink=$_W ['siteroot'] . 'app/' . $this->createMobileUrl ( 'index', array ('rid' => $rid,'fromid'=>$openid));
	}
	$sharelink = str_replace ( './', '', $sharelink );//分享链接
	$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
	
include $this->template('ranking');